from webob import Response
from fusion.http.controllers import BaseController

import sys
import os

class Static(BaseController):
    def index(self, req):
        # try to send back a file from the static directory
        #[TODO: make this more configurable/resistant to changes]
        static_dir = os.path.join('./fusion/http/static/')
        static_path = req.urlvars['fusion.path'].replace('/..', '')
        path = os.path.join(static_dir, static_path) 

        print "WILL TRY TO GET STATIC %s [%s]" % (path, os.path.isfile(path))
        if os.path.isfile(path):
            #[FIXME: file_wrapper?]
            '''
            fd = file(path)
            if 'wsgi.file_wrapper' in self.environ:
                return self.environ['wsgi.file_wrapper'](fd)
            else:
                return iter(lambda: fd.read(8192), '')
            '''
            mime_type = self._get_mime_type(path)
            res = Response(content_type=mime_type)
            res.body = open(path, 'rb').read()

            return res
        else:
            print "ERROR0"
            return None
        
 

